// pages/TradingArea/TradingArea.js\
const controller = require("../controllers/Controller").controller

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar: [],
    lat: "",
    lng: "",
    imageList: "",
    activityList: "",
    pageNumber: 1,
    pageSize: 10,
    address: "",
    name: "",
    lastPage: false,
    scrollIndex: 0,
    redTimer:"",
    redShow:false,
    closeAnimate:false,
    isReceived:false,
    redText:""
  },
  //阻止冒泡
  stop(){
    
  },
  backPage(){
    wx.navigateBack({
      delta: 0,
    })
  },

  getScrollLeft(e) {
    let scrollLeft = e.detail.scrollLeft + 26
    let scrollIndex = parseInt(scrollLeft / 128)
    console.log(scrollLeft)
    console.log(scrollIndex)
    this.setData({
      scrollIndex: scrollIndex
    })
  },

  toShopCircleDetail(e) {
    let id = e.currentTarget.dataset.id
    let pinNum=e.currentTarget.dataset.pin
    let name = e.currentTarget.dataset.name
    let shamPinNum=e.currentTarget.dataset.sapin

    wx.reLaunch({
      url: '/pages/TradingArea/index?id=' + id + "&name=" + name+"&pinNum="+pinNum+"&shamPinNum="+shamPinNum,
    })
  },


  //获取商圈列表
  getMyShopCircle() {
    let that=this
    let value = {}
    value.name = this.data.name
    value.pageNumber = this.data.pageNumber
    value.lng = app.globalData.lng || ''
    value.lat = app.globalData.lat || ''
    value.pageSize = this.data.pageSize
    controller.getMyShopCircle(value).then(res => {
      this.setData({
        list: res.data.list,
        lastPage: res.data.lastPage
      })
      // that.selectTransactionRed()
    })
  },

  getMoreShopCircle() {
    let value = {}
    if (this.data.lastPage == false) {
      value.pageNumber = this.data.pageNumber + 1
      this.setData({
        pageNumber: value.pageNumber
      })
      value.name = this.data.name
      value.lng = app.globalData.lng || ''
      value.lat = app.globalData.lat || ''
      value.pageSize = this.data.pageSize
      controller.getMyShopCircle(value).then(res => {
        let list = this.data.list
        let list2 = res.data.list
        let list3 = list.concat(list2)
        this.setData({
          list: list3,
          lastPage: res.data.lastPage
        })
      })
    }
  },

  getAddress() {
    let that = this
    if(!app.globalData.lat){
      wx.getLocation({
        success: function (e) {
          console.log(e)
          that.setData({
            lat: e.latitude,
            lng: e.longitude
          })
  
          app.globalData.lat = e.latitude
          app.globalData.lng = e.longitude
          that.getMyShopCircle()
          that.getDetailAddress()
        },
        fail: function (err) {
          console.log('getLocation',err)
        }
      })
    }else{
      that.setData({
        lat: app.globalData.lat,
        lng: app.globalData.lng
      })
      that.getMyShopCircle()
      that.getDetailAddress()
    }
  },

  getDetailAddress() {
    let that = this
    let value = {}
    value.lat = app.globalData.lat
    value.lng = app.globalData.lng
    controller.getAddressDeatil(value).then(res => {
      this.setData({
        address: res.data
      })
      that.getImageList()
    })
  },


  //获取轮播图
  getImageList() {
    controller.getImageList({
      type: 15,
      districtId: this.data.address.id
    }).then(res => {
      this.setData({
        imageList: res.data
      })
    })
  },

  //重新设置地址授权
  openSet() {
    
    let that = this
    wx.openSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation']) {
          that.getAddress()
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this
    this.setData({
      tabbar: app.globalData.tabbar3
    })
    // this.getImageList()
    
    // 消息通知进入直接到对应商圈
    if(options.name){
      wx.reLaunch({
        url: '/pages/TradingArea/index?id=' + options.id + "&name=" + options.name+"&pinNum="+options.pinNum+"&shamPinNum="+options.shamPinNum,
      })
    } 
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that=this
    this.setData({
      pageNumber: 1
    })
    //请求红包并判断是否可以开启定时器

    if (wx.getStorageSync('usertoken')) {
      //加载首页数据
      this.getAddress()
    }else{
      //无usertoken，等待login登录回调
      app.usertokenReadyCallback = res => {
        if(res.data.sessionId){
          this.getAddress()
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that=this
    console.log("清除计时器")
    clearInterval(that.data.redTimer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // this.getMore()
    this.getMoreShopCircle()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  //切换tab
  tabChange(e) {
    let index = e.detail.index
    if(index === 0){
      wx.requestSubscribeMessage({
        tmplIds: ['uDgukjuL1wdlg-ZTmt6iEOHRCDni2M1g0qsaJByYxgw',
        'CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0',
        'Gc_gkdb1wX-cSBFPD4D5D31w6ZOojJeDckkzo49pBmQ'
        ],
      })
      wx.redirectTo({
        url: '/pages/TradingArea/index',
      })
    }
    else if (index === 1) {
      wx.redirectTo({
        url: '/pages/TradingArea/TradingArea',
      })
    } else if (index == 2) {
      wx.redirectTo({
        url: '/pages/Chain/Chain',
      })
    } else {
      wx.redirectTo({
        url: '/pages/myCenter/myCenter',
      })
    }
  },
  //去更多商圈
  toShopCircle() {
    wx.navigateTo({
      url: '/pages/TradingArea/moreShopCircle',
    })
  },

  toShopCircle2() {
    let districtId = this.data.address.id
    wx.navigateTo({
      url: '/pages/TradingArea/moreShopCircle?districtId=' + districtId,
    })
  },

  //搜索商圈
  getShopCircleName(e) {
    let name = e.detail.value
    this.setData({
      name: name
    })
  },

  //跳转更多
  toMore(e) {
    let id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type
    wx.navigateTo({
      url: '/pages/TradingArea/ActivityDetail?id=' + id + "&type=" + type,
    })
  },

  //跳转券包活动详情
  toGiftBagDetail(e) {
    let id = e.currentTarget.dataset.id
    let shopGroupId = e.currentTarget.dataset.shopgroupid
    wx.navigateTo({
      url: '/pages/TradingArea/GiftBagDetail?id=' + id + "&shopGroupId=" + shopGroupId + "&type=0",
    })
  },
  //跳转礼包活动详情
  toGiftBagDetail2(e) {
    let id = e.currentTarget.dataset.id
    let shopGroupId = e.currentTarget.dataset.shopgroupid
    let ids = e.currentTarget.dataset.ids || 'c'
    let list = ids.split(",")
    console.log(list)
    // if(list.length<=1){
    wx.navigateTo({
      url: '/pages/TradingArea/GiftBagDetail?id=' + id + "&shopGroupId=" + shopGroupId + "&type=1",
    })
  },

   //跳转红包店铺列表
   toRedBagShopList(e){
     let id=e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/pages/TradingArea/redBagShopList?id='+id,
      })
  }
})